package com.d3.pomodoro.component

import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.LinearLayout
import android.widget.TextView
import com.d3.pomodoro.R
import com.d3.pomodoro.utils.SoftKeyBoardController
import kotlinx.android.synthetic.main.view_bottom.view.*

class BottomView : LinearLayout, View.OnClickListener {

    var currModel: ShowModel = ShowModel.NORMAL_MODEL

    private var bottomViewListener: BottomViewListener? = null

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    enum class ShowModel {
        EDIT_MODEL, NORMAL_MODEL
    }

    enum class ActionType {
        ACTION_CLICK_DONE, ACTION_CLICK_COMPLETE,
        ACTION_CLICK_CLOCK, ACTION_CLICK_CALENDAR,
        ACTION_CLICK_RANKING, ACTION_CLICK_STATISTICS,
        ACTION_CLICK_SETTING, ACTION_CLICK_CLOSE_INPUT
    }


    init {
        inflate(context, R.layout.view_bottom, this)
        imageClock.setOnClickListener(this)
        imageCalendar.setOnClickListener(this)
        imageRankingList.setOnClickListener(this)
        imageStatistics.setOnClickListener(this)
        imageSetting.setOnClickListener(this)

        imageCloseInput.setOnClickListener(this)
        imageComplete.setOnClickListener(this)


        editTask.apply {
            setOnEditorActionListener(object : TextView.OnEditorActionListener {
                override fun onEditorAction(
                    v: TextView?,
                    actionId: Int,
                    event: KeyEvent?
                ): Boolean {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        //click done
                        bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_DONE)
                        return true
                    }
                    return false
                }

            })
        }

    }

    override fun onClick(v: View?) {
        when {
            v?.id == R.id.imageCalendar -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_CALENDAR)
            }
            v?.id == R.id.imageClock -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_CLOCK)
            }
            v?.id == R.id.imageCloseInput -> {
                //关闭软键盘
                SoftKeyBoardController.hideSoftKeyboard(context, editTask)
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_CLOSE_INPUT)
            }
            v?.id == R.id.imageComplete -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_COMPLETE)
            }
            v?.id == R.id.imageRankingList -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_RANKING)
            }
            v?.id == R.id.imageSetting -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_SETTING)
            }
            v?.id == R.id.imageStatistics -> {
                bottomViewListener?.action(actionType = ActionType.ACTION_CLICK_STATISTICS)
            }
        }
    }


    fun changeModel(model: ShowModel) {
        if (currModel == model) {
            return
        }
        currModel = model
        refreshViewByModel(currModel)
    }

    fun refreshViewByModel(model: ShowModel) {
        if (model == ShowModel.EDIT_MODEL) {
            llBehaviourView.visibility = View.GONE
            llEditView.visibility = View.VISIBLE
        } else {
            llBehaviourView.visibility = View.VISIBLE
            llEditView.visibility = View.GONE
        }
    }

    fun cleanInput() {
        editTask.setText("")
    }

    fun setBottomViewListener(bottomViewListener: BottomViewListener?) {
        this.bottomViewListener = bottomViewListener
    }

    interface BottomViewListener {
        fun action(actionType: ActionType)
    }

}